package com.gxuwz.controller;

import com.gxuwz.entity.Classes;
import com.gxuwz.entity.Student;
import com.gxuwz.entity.vo.PageVo;
import com.gxuwz.result.Code;
import com.gxuwz.result.PageInfo;
import com.gxuwz.result.ResultInfo;
import com.gxuwz.service.ClassesService;
import com.gxuwz.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Description:学生信息管理模块
 */

@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private ClassesService classesService;

    /**
     * @Description:查询学生信息，跳转至学生信息管理页面
     * @Param:  [com.gxuwz.result.PageInfo, org.springframework.web.servlet.ModelAndView, com.gxuwz.entity.Student]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @GetMapping("/list")
    public ModelAndView departmentList(PageInfo pageInfo, ModelAndView modelAndView, Student student, HttpServletRequest request){
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        PageVo pageVo = studentService.getStudentList(student, pageInfo,username);
        modelAndView.setViewName("studentManage/studentManage");
        modelAndView.addObject("pageVo",pageVo);
        return modelAndView;
    }

    /**
     * @Description:实现添加学生信息功能，并跳转至学生信息管理页面
     * @Param:  [org.springframework.web.servlet.ModelAndView, com.gxuwz.entity.Student]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @RequestMapping("/addStudent")
    public ModelAndView addDepartment(ModelAndView modelAndView,Student student,HttpServletRequest request){
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        boolean flag = studentService.addStudentInfo(student);
        PageInfo pageInfo = new PageInfo();
        if(flag){
            modelAndView.addObject("resultInfo",new ResultInfo(Code.ADD_OK,"新增成功"));
        } else {
            modelAndView.addObject("resultInfo",new ResultInfo(Code.ADD_ERR,"新增失败"));
        }
        PageVo pageVo = studentService.getStudentList(null, pageInfo,username);
        modelAndView.setViewName("studentManage/studentManage");
        modelAndView.addObject("pageVo",pageVo);
        return modelAndView;
    }

    /**
     * @Description:实现修改学生信息功能，并跳转至学生信息管理页面
     * @Param:  [org.springframework.web.servlet.ModelAndView, com.gxuwz.entity.Student, java.lang.String]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @RequestMapping("/updateStudent")
    public ModelAndView updateDepartment(ModelAndView modelAndView,Student student,String oldId,HttpServletRequest request){
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        boolean flag = studentService.updateStudentInfo(student,oldId);
        PageInfo pageInfo = new PageInfo();
        if(flag){
            modelAndView.addObject("resultInfo",new ResultInfo(Code.UPDATE_OK,"修改成功"));
        } else {
            modelAndView.addObject("resultInfo",new ResultInfo(Code.UPDATE_OK,"修改失败"));
        }
        PageVo pageVo = studentService.getStudentList(null, pageInfo,username);
        modelAndView.setViewName("studentManage/studentManage");
        modelAndView.addObject("pageVo",pageVo);
        return modelAndView;
    }

    /**
     * @Description:实现查询相应学生信息，并跳转至修改学生信息页面功能
     * @Param:  [java.lang.String, org.springframework.web.servlet.ModelAndView]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @GetMapping("/updateStudentPage")
    public ModelAndView queryDepartmentOne(String stuID,ModelAndView modelAndView,HttpServletRequest request){
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        Student studentOne = studentService.getStudentOne(stuID);
        List<Classes> classesList = classesService.queryClassesIdAndClassName(username);
        modelAndView.addObject("classes",classesList);
        modelAndView.addObject("student",studentOne);
        modelAndView.setViewName("studentManage/editStudent");
        return modelAndView;
    }

    /**
     * @Description:实现根据ID删除相应学生信息功能
     * @Param:  [java.lang.String, org.springframework.web.servlet.ModelAndView]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @RequestMapping("/deleteStudentOne")
    public ModelAndView deleteDepartmentOne(String stuID, ModelAndView modelAndView,HttpServletRequest request){
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        boolean flag = studentService.deleteStudentOne(stuID);
        PageInfo pageInfo = new PageInfo();
        if(flag){
            modelAndView.addObject("resultInfo",new ResultInfo(Code.DELETE_OK,"删除成功"));
        } else {
            modelAndView.addObject("resultInfo",new ResultInfo(Code.DELETE_ERR,"删除失败"));
        }
        PageVo pageVo = studentService.getStudentList(null, pageInfo,username);
        modelAndView.setViewName("studentManage/studentManage");
        modelAndView.addObject("pageVo",pageVo);
        return modelAndView;
    }

    /**
     * @Description:实现批量删除学生信息功能
     * @Param:  [java.lang.String[], org.springframework.web.servlet.ModelAndView]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @RequestMapping("/deleteStudents")
    public ModelAndView deleteDepartments(String[] ids,ModelAndView modelAndView,HttpServletRequest request){
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        System.out.println(ids);
        boolean flag = studentService.deleteStudents(ids);
        PageInfo pageInfo = new PageInfo();
        if(flag){
            modelAndView.addObject("resultInfo",new ResultInfo(Code.DELETE_OK,"批量删除成功"));
        } else {
            modelAndView.addObject("resultInfo",new ResultInfo(Code.DELETE_ERR,"批量删除失败"));
        }
        PageVo pageVo = studentService.getStudentList(null, pageInfo,username);
        modelAndView.setViewName("studentManage/studentManage");
        modelAndView.addObject("pageVo",pageVo);
        return modelAndView;
    }

    /**
     * @Description:实现查询学生所拥有的课程信息功能
     * @Param:  [org.springframework.web.servlet.ModelAndView]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @RequestMapping("/queryClassesInfo")
    public ModelAndView queryClassesInfo(ModelAndView modelAndView,HttpServletRequest request){
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        List<Classes> classesList = classesService.queryClassesIdAndClassName(username);
        modelAndView.setViewName("studentManage/addStudent");
        modelAndView.addObject("classes",classesList);
        return modelAndView;
    }
}
